package com.zy.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zy.activity.adapter.BaseFragmentAdapter
import com.zy.activity.adapter.FragmentListBean
import com.zy.utils.ThreadUtils
import java.util.ArrayList

abstract class BaseListFragment : Fragment(), BaseFragmentAdapter.ClickCallBack {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(layout, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val rv = view.findViewById<RecyclerView>(R.id.base_rv)
        rv.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        val list: ArrayList<FragmentListBean> = ArrayList()
        init(list)
        val adapter = BaseFragmentAdapter(list)
        rv.adapter = adapter
        adapter.setClickCallBack(this)

    }


    override fun click(bean: FragmentListBean) {
        clickItem(bean)
    }

    protected abstract fun init(list: ArrayList<FragmentListBean>)
    protected abstract fun clickItem(bean: FragmentListBean)

    protected open val layout: Int
        protected get() = R.layout.fragment_list_base


    protected fun runPost(long: Long, run: Runnable) {
        ThreadUtils.mainPostDelayed(long, run)
    }

    companion object {
        private const val TAG = "BaseListFragment"
    }
}